package com.imau.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imau.bean.Permission;
import com.imau.bean.Role;

import com.imau.bean.RolePermission;
import com.imau.common.utils.SqlSessionFactoryUtil;
import com.imau.common.utils.StringLike;
import com.imau.mapper.*;

import com.imau.service.RoleService;
import io.mybatis.mapper.example.Example;
import io.mybatis.service.AbstractService;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;


public class RoleServiceImpl extends AbstractService<Role, String, RoleMapper> implements RoleService {


    private RolePermissionMapper rolePermissionMapper;

    private RoleMapper roleMapper;

    private PermissionMapper permissionMapper;

    private SqlSession session;

    private void closeMapper() {

        if (session != null) {
            session.close();
        }
    }

    private void initMapper() {


        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        session = factory.openSession(true);

        baseMapper = session.getMapper(RoleMapper.class);
        roleMapper = session.getMapper(RoleMapper.class);
        rolePermissionMapper = session.getMapper(RolePermissionMapper.class);
        permissionMapper = session.getMapper(PermissionMapper.class);


    }

    @Override
    public Role save(Role role) {


        initMapper();
        Role save = super.save(role);

        //维护中间表权限
        assignPermission(role.getId(), role.getPermissions());
        closeMapper();

        return save;

    }

    @Override
    public int deleteById(String id) {


        initMapper();

        int i = baseMapper.deleteByPrimaryKey(id);

        rolePermissionMapper.delete(new RolePermission(id));

        closeMapper();

        return i;
    }

    @Override
    public int updateRole(Role role) {

        initMapper();

        int i = baseMapper.updateByPrimaryKey(role);
        //更新分配角色
        assignPermission(role.getId(), role.getPermissions());

        closeMapper();
        return i;
    }

    @Override
    public Role findById(String id) {

        initMapper();
        //1.查询用户

        Role role = super.findById(id);//baseMapper.selectByKey(did);

        //2. 查询角色信息


        List<RolePermission> rolePermissions = rolePermissionMapper.selectList(new RolePermission(id));

        //2.2 查询权限 菜单+按钮
        ArrayList<Integer> types = new ArrayList<>();
        types.add(1);
        types.add(2);

        List<Permission> permissions = permissionMapper.selectByFieldList(Permission::getType, types);

        role.getPermissions().addAll(permissions);


        session.close();

        return role;
    }


    @Override
    public PageInfo<Role> queryListByPagte(Role Role, int currentPage, int size) {

        SqlSessionFactory factory = SqlSessionFactoryUtil.getInstace();

        SqlSession session = factory.openSession(true);

        baseMapper = session.getMapper(RoleMapper.class);

        //1. 查询前构建分页对象

        PageHelper.startPage(currentPage, size);

        //2. 构建查询参数

        //2.1 创建 example 对象
        Example<Role> example = new Example<Role>();

        //2.2 创建 动态拼接Sql对象
        Example.Criteria<Role> criteria = example.createCriteriaSelective();

        if (Role != null) {
            criteria.andLike(com.imau.bean.Role::getName, StringLike.strCaonstLike(Role.getName()));


        }
        //2.3 查询
        List<Role> Roles = baseMapper.selectByExample(example);
        //2.4 封装成pageInfo 对象
        PageInfo<Role> pageInfo = new PageInfo<>(Roles);

        session.close();
        return pageInfo;
    }

    @Override
    public int assignPermission(String rid, String[] perids) {


        int row = 0;

        //1.先删除中间表
        rolePermissionMapper.delete(new RolePermission(rid));


        //2.插入中间表

        for (String pid : perids) {


            row += rolePermissionMapper.insert(new RolePermission(rid, pid));

        }

        closeMapper();

        return row;
    }

    public int assignPermission(String rid, Set<Permission> permissionSet) {


        int row = 0;

        //1.先删除中间表
        rolePermissionMapper.delete(new RolePermission(rid));


        //2.插入中间表

        for (Permission permission : permissionSet) {


            row += rolePermissionMapper.insert(new RolePermission(rid, permission.getId()));

        }

        closeMapper();

        return row;
    }
}
